#!/usr/bin/perl

use strict;
use autodie;

use Getopt::Std;

use File::Spec;


# my $log = ".log";
# open LOG, ">>$log";

# my %opts =('a', 1, 'i', 0);
my %opts =('i', "none");

getopts('i:f:Ac:', \%opts);

my($intv, $cutadp, $cnvcal, $cnvcal_ref, %sap_name, @sapl_name, %lane_num, %lane_series, $com, $time_rand);

if (defined $ARGV[1]) {
	$time_rand = $ARGV[1];
}else {
	$time_rand = `date +%s-%N`;
}

my $reads_list = $time_rand . ".list";


if ( defined $opts{f}) {
	if ($opts{f} =~ /\.bed$/) {
		$intv = $opts{f};
	}else {
		# $intv = undef;
		die "Error: invalid interval file, should be a bed file.\n"
	}
	
}elsif ($opts{i} eq "none") {
	$intv = "genome";
}elsif ($opts{i} eq "brca12") {
	$intv = "brca12";
}elsif ($opts{i} eq "brca44") {
	$intv = "brca44";
}elsif ($opts{i} eq "luca50ph") {
	$intv = "luca50ph";
}elsif ($opts{i} eq "brca12ph") {
	$intv = "brca12ph";
}elsif ($opts{i} eq "brca12mor") {
	$intv = "brca12mor";
}elsif ($opts{i} eq "ca50mor") {
	$intv = "ca50mor";
}elsif ($opts{i} eq "ctc80") {
	$intv = "ctc80";
}else {
	# $intv = undef;
	die "Error: invalid option -i input.\n";
}


if ($opts{A}) {
	$cutadp = 0;
}else {
	$cutadp = 1;
}

if (defined $opts{c}) {
	if ($opts{'c'} =~ /.bam/) {
		$cnvcal = 1;
		$cnvcal_ref = $opts{'c'};
	}else {
		die "Error: the reference data of a cnv calling must be a bam file.\n";
	}

}else {
	$cnvcal = 0;
}

# print "interval: $intv\n";
# print "cut adaptor: $cutadp\n";
# print "cnv calling: $cnvcal\n";
# my @ke = keys %opts;
# my @va =values %opts;
# print "@ke\n";
# print "@va\n";
# print "$opts{'c'}\n";

my $dir = $ARGV[0];
$dir= File::Spec->rel2abs( $dir );

my $abs_path;

if (File::Spec->file_name_is_absolute( $dir )) {

	$abs_path = $dir;
}else {
	$abs_path = File::Spec->rel2abs( $dir );
}


# open READS, ">$reads_list";
my $dirh;
opendir $dirh, $dir or die "Error: cannot open $dir: $!";


foreach (readdir $dirh) {
	# my $i = 0;
	if (/^([a-zA-Z0-9\-]+)_[ATGCatgc]+_(L[\d][\d][\d])_R1_([\d][\d][\d])\.(fastq|fq)(\.gz)?/ ||
	/^([a-zA-Z0-9\-]+)_[sS]\d+_(L[\d][\d][\d])_R1_([\d][\d][\d])\.(fastq|fq)(\.gz)?/ || /^([a-zA-Z0-9\-]+)_[Rr]1\.(fastq|fq)(\.gz)?/) {
		my ($nam, $lane, $series) = ($1, $2, $3);
		$sap_name{$1}++;
		$lane_num{$2}++;
		$lane_series{"$2s$3"}++;
		# if (/$lane/) {
			
			# $lane_series[$i] = $3;
		# }
		
		# $i++;
		# my ($lane_num0, $read_num);
		# if ($2 =~ /([1-9]+)/ {
			# $lane_num0 = $1;
		# }else {
			# die "Error: irregular reads name.\n"
		# }
		
		# if ($3 =~ /([1-9]+)/ {
			# $lane_num0 = $1;
		# }else {
			# die "Error: irregular reads name.\n"
		# }
		
		# ($read_num = $3) =~ /[1-9]+/;
		# ($lane_num0 = $2) =~ /[1-9]+/;
		
		my $file = File::Spec->catfile(($abs_path), $_);
		
		# print "\$3: $3\n";
		# print READS "$file\t";
		# print "$file\t";
		s/_R1/_R2/;
		# print READS "$file\t$nam\t${lane}s$series\n";
		# print "$file\t$nam\t${lane}s$series\n";

	}
}





closedir $dirh;

# close READS;

@sapl_name = keys %sap_name;





# unlink "$reads_list" and die "Error: The reads directory must has one and only one sample's reads data. the name of reads must be style like NAME_CAGATC_L001_R1_001.fastq[.gz].\n" if @sapl_name != 1;
	
die "Error: The reads directory must have one and only one sample's reads data. The name of reads must be style like NAME_CAGATC_L001_R1_001.fastq[.gz] or NAME_S1_L001_R1_001.fastq[.gz] or NAME_R1.fastq[.gz] in which NAME can have '-' in it like NA-ME.\n" if @sapl_name != 1;
	
	
# $reads_list_new = $sapl_name[0] . "_" . $reads_list;

# rename $reads_list, $reads_list_new; # if many samples run in the same directory and if one sample's run state is Q, then the second run will override the first reads_list if the name is equal.

my @nam = keys %lane_num;
my @num = values %lane_num;
my @series = keys %lane_series;
# print "@nam\n";
# print "@num\n";


	
	
# open READS1, "$reads_list";

# while (<READS1>) {
	# chomp;
	# my @reads = split /\t/;
	# my $reads1 = $reads[0];
	# my $reads2 = $reads[1];
	# my $sap_name = $reads[2];
	# my $base = $reads[3];
	# print "\n";
	# print "$reads1\n";
	# print "$reads2\n";
	# print "$base\n";
	# print "\n";
	
# }

# close READS1;

	
# merge
# system 'merge $lane_num'
# while (my ($lane, $num) = each %lane_num) {
		
		# my $com;
		# merge by lane
		# if ($num > 1) {
		
			
			
			# foreach (@series) {
				# if (/$lane/) {
					# $com .= "$_ ";
				# }
			# }
			# print "$com\n";
			# system "echo merge ...";
		# }else {
		
			# foreach (@series) {
				# if (/$lane/) {
					# $com = $_;
				# }
			# }
			# print "$com\n";
			# system "echo no merge ...";
			
			# rename $com, "1";
		# }
# }
	
	
# print "@series\n";

open NAM, ">$time_rand";

print NAM "$sapl_name[0]";

close NAM;
